<template>
<div class="header">
    <div class="header-container">
        <div class="container">
            <div class="logo">
                <router-link :to="{name:'Home'}">
                    <img src="@/assets/logo.png" alt="">
                </router-link>
            </div>
            <ul class="nav">
                <li><router-link :to="{name:'Home'}">首页</router-link></li>
                <li v-if="isLoading"><a href="">loading...</a></li>
                <li v-for="item in data.slice(0,5)" :key="item.channelId">
                    <router-link :to="{name:'ChannelNews',params:{id:item.channelId}}">{{item.name}}</router-link>
                </li>
            </ul>
            <div class="user">
                <span v-if="loginLoading">loading</span>
                <template v-else-if="loginData">
                    <a href="" @click.prevent="$router.push({name:'PersonInfo'})">{{loginData.nickname}}</a>
                    <a href="" @click.prevent="handleLogout">退出登录</a>
                </template>
                <template v-else>
                    <router-link :to="{name:'Login'}">登录</router-link>
                    <router-link :to="{name:'Reg'}">注册</router-link>
                </template>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import {mapState} from "vuex"

export default {
    computed:{
        ...mapState("channels",["data","isLoading"]),
        ...mapState("loginUser",{
            loginData:"data",
            loginLoading:"isLoading"
        })
    },
    created() {
        // console.log(mapState("channels",["data","isLoading"]))
    },
    methods:{
        handleLogout(){
            this.$store.dispatch("loginUser/logOut")
            this.$router.push({name:"Login"})
        }
    }
}
</script>

<style scoped>
    ul{
        list-style: none;
    }
    a{
        text-decoration: none;
        color: white;
    }
    .header-container{
        background-color: #333;
    }
    .container{
        width: 1080px;
        margin: 0 auto;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .container .logo{
        width: 40px;
    }
    .container .logo img{
        width: 100%;
    }
    .nav{
        width: 700px;
        display: flex;
        justify-content:space-evenly;
    }
    .user{
        display: flex;
        justify-content:flex-end;
        align-items: center;
        width: 80px;
        color:white;
        width: 150px;
    }
    .user a{
        margin-left: 10px;
    }
    .user a:first-child{
        width: 60px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        text-align: right;
    }
    .router-link-exact-active{
        color: gold;
    }
</style>